<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 加载在状态 -->
    <van-loading class="loading" type="spinner" color="#1989fa" size="0.64rem" v-if="articleInfoObj.title===undefined">加载中...</van-loading>
  <!-- 文章及加载 -->
   <div v-else>
        <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ articleInfoObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="articleInfoObj.aut_name"
        :label="articleInfoObj.pubdate"
      >
        <template #icon>
          <img :src="articleInfoObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="articleInfoObj.is_followed"
              @click="followedFn"
            >
              已关注
            </van-button>
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followedFn"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleInfoObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="articleInfoObj.attitude === -1 ? 0 : articleInfoObj.attitude"
          @click="collectedFn(false)"
          >不喜欢</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="collectedFn(true)"
          >喜欢</van-button
        >
      </div>
    </div>
    <!-- 评论区域 -->
    <div>
      <CommentList
        :aitId="articleInfoObj.art_id"
        v-if="articleInfoObj.art_id"
      ></CommentList>
    </div>
   </div>
  </div>
</template>

<script>
import {
  articleInfoAPI,
  followingAPI,
  unfollowingAPI,
  unLikingsAPI,
  likingsAPI
} from '@/api'
import CommentList from './CommentList'
// 自己封装的通知插件
import { MyNotify } from '@/utils/Notify'
export default {
  name: 'ArticleDetail',
  data () {
    return {
      articleInfoObj: {} // 文章对象
    }
  },
  methods: {
    //   关注作者
    async followedFn () {
      if (this.articleInfoObj.is_followed === false) {
        this.articleInfoObj.is_followed = true
        const res = followingAPI({ target: this.articleInfoObj.aut_id })
        console.log(res)
      } else {
        this.articleInfoObj.is_followed = false
        const res2 = await unfollowingAPI({ id: this.articleInfoObj.aut_id })
        console.log(res2)
      }
    },
    // 喜欢文章
    async collectedFn (bool) {
      if (bool === true) {
        this.articleInfoObj.attitude = -2
        const res2 = await likingsAPI({ target: this.articleInfoObj.art_id })
        console.log(res2)
      } else {
        this.articleInfoObj.attitude = -1
        const res = await unLikingsAPI({ id: this.articleInfoObj.art_id })
        console.log(res)
      }
    }
  },
  async created () {
    try {
      const res = await articleInfoAPI({ id: this.$route.query.aid })
      this.articleInfoObj = res.data.data
    } catch (error) {
      if (error.response.status === 404) {
        MyNotify({ type: 'warning', message: '文章不存在' })
        setTimeout(() => {
          this.$router.back()
        }, 5000)
      }
    }
  },
  components: {
    CommentList
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.loading{
    padding-top: 50px;
    text-align: center;
}
</style>
